## setup ##

# clean environment
rm(list=ls())

# load packages
library(sandwich)
library(lmtest)
library(tidyverse)
library(xtable)
library(readxl)
library(stargazer)
library(stringr)
library(lubridate)

# load main data
dat <- read.csv2("data_experiment_outcomes_csv.csv",
                 encoding = 'UTF-8')

## assign correct types (just to be safe)
# outcome variable to numeric
dat$response <- as.numeric(as.character(dat$response))

# IVs to numeric
dat$immback <- as.numeric(as.character(dat$immback))
dat$partisan <- as.numeric(as.character(dat$partisan))


# Figure A5: Subset to responses we received during the first day

  # HH:MM:SS Regex for StringR to extract times
hhmmss <- "([0-9]{2})[:punct:]([0-9]{2})[:punct:]([0-9]{2})"

dat$sent_hms <- hms( str_extract(dat$date_sent.x, hhmmss) )

dat$receive_hms <- hms( str_extract(dat$date_received.x, hhmmss) )

dat$diff_hms <- as.duration (dat$receive_hms - dat$sent_hms)

# define cutoffs
hours <- c(2,4,6,8,10,12)


 dat$day_diff <- as.numeric(dat$day_diff)
 dat$day_sent <- as.numeric(dat$day_sent)
 dat$month_sent <- as.numeric(dat$month_sent)

# keep emails that where dispatched on day 1
dat <- dat %>% filter(day_sent == 09 & month_sent == 07 )

immback_sent <- sum(dat$immback == 1)
ger_sent <- sum (dat$immback == 0)


# keep emails for which we received a response within day 1
dat <- dat %>% filter(day_sent == 09 & month_sent == 07 & day_diff == 0)


ests <- tibble()

for(i in 1:length(hours)) {
  print(i)
  dat_loop <- dat %>% mutate(
    response = ifelse(  diff_hms <= dhours(x = hours[i]),
                        response, NA) )

  immback_rec <- dat_loop %>% filter(immback == 1 & response == 1) %>% 
    summarise(resp = sum(response))  %>% 
    mutate(resp_share = resp / immback_sent,
           sent = immback_sent) %>% 
    mutate(cutoff = paste(hours[i], 'hours'),
           condition= 'immback')
  
  ger_rec <- dat_loop %>% filter(immback == 0 & response == 1) %>% 
    summarise(resp = sum(response)) %>% 
    mutate(resp_share = resp / ger_sent,
           sent = ger_sent) %>% 
    mutate(cutoff = paste(hours[i], 'hours'),
           condition= 'ger')
  
  diff <- ger_rec$resp_share - immback_rec$resp_share
  print(diff)
  
  ests <-  rbind(ests, immback_rec, ger_rec)  
}

ests <- drop_na(ests)

pd <- position_dodge(1)

ests <- ests %>%
  rename("Response Share" = resp_share,
         Period = cutoff)

# Define factor order for plot:
ests$Period <- ordered(ests$Period, levels = c(  "2 hours", "4 hours", "6 hours",   "8 hours", "10 hours",  "12 hours" ))


p1 <- ggplot(ests, aes(x=Period, y=`Response Share`, fill=condition)) + 
  geom_bar(stat="identity", position=position_dodge()) +
  scale_fill_manual(values=c("lightblue", "red"), name="Share of Responses \nby Immigrant Status",
                    labels = c( "German", "Turkish")) +
  xlab(element_blank()) + ylab("Share of Responses") +
  theme_bw()   + theme(panel.grid = element_blank(), legend.position = "bottom",
                       text = element_text(size=14)) 

p1

# # save
# ggsave('fig_A5.pdf',
#        width=7, height=5)

